package outoforder.leetcode01;

import org.junit.Test;

import java.util.ArrayDeque;

/**
 * @author shhjiang_001
 * @create 2020-07-13 16:23
 */
// 区间列表的交集
public class Solution986 {
    public int[][] intervalIntersection(int[][] A, int[][] B) {
        int i = 0;
        int j = 0;
        ArrayDeque<int[]> list = new ArrayDeque<>();
        while (i < A.length && j < B.length){
            int a1 = A[i][0];
            int a2 = A[i][1];
            int b1 = B[j][0];
            int b2 = B[j][1];
            if (b2 >= a1 && a2 >= b1){
                list.addLast(new int[]{Math.max(a1,b1),Math.min(a2,b2)});
            }
            if (b2 < a2){
                j++;
            }else {
                i++;
            }
        }
        int[][] res = new int[list.size()][2];
        int index = 0;
        while (!list.isEmpty()){
            res[index] = list.pop();
            index++;
        }
        return res;
    }
    @Test
    public void test(){
        intervalIntersection(new int[][]{{0,2},{5,10},{13,23},{24,25}},new int[][]{{1,5},{8,12},{15,24},{25,26}});
    }
}
